/**
 * 
 */
package info.niwota.home.fm;

import info.niwota.home.R;
import instant.openvfs.home.Debug;

import java.net.URI;

import x.java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class LinkActivity extends BaseFileActivity {
	private static final String LOG_TAG = "LinkActivity";
	private EditText inputText;
	private EditText selectText;
	private String path;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTheme(android.R.style.Theme_Translucent_NoTitleBar);
		setContentView(R.layout.link);

		init();
	}

	private void init() {
		final TextView pathText = (TextView) findViewById(R.id.link_path);
		inputText = (EditText) findViewById(R.id.link_input);
		selectText = (EditText) findViewById(R.id.link_uri);

		final Button confirmBtn = (Button) findViewById(R.id.link_confirm);
		final Button cancelBtn = (Button) findViewById(R.id.link_cancel);
		final ImageButton viBtn = (ImageButton) findViewById(R.id.link_voiceinput);
		final ImageButton fcBtn = (ImageButton) findViewById(R.id.link_filechoose);
		//
		Uri data = getIntent().getData();
		path = data.getPath();
		pathText.setText(path);

		inputText.setText("");
		//
		confirmBtn.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
		viBtn.setOnClickListener(this);
		fcBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.link_voiceinput:
			startVoiceInputActivityForResult();
			break;
		case R.id.link_filechoose:
			startFileChooseActivityForResult();
			break;
		case R.id.link_confirm:
			doConfirm();
			break;
		case R.id.link_cancel:
			doCancel();
			break;
		default:
		}
	}

	private void doCancel() {
		setResult(RESULT_CANCELED);
		finish();
	}

	private void doConfirm() {
		try {
			final CheckBox read = (CheckBox) findViewById(R.id.link_optionRead);
			final CheckBox write = (CheckBox) findViewById(R.id.link_optionWrite);
			String name = inputText.getText().toString();
			name = Utils.sanitize(name);

			URI uri = new URI(selectText.getText().toString());
			if (uri.isAbsolute() == false) {
				uri = new URI("file:" + path);
			}
			String scheme = uri.getScheme();
			if (scheme.equals("v")) {
				uri = new File(uri).toRealURI();
			}
			String options = (read.isChecked() ? "r" : "")
					+ (write.isChecked() ? "w" : "");
			options = (options.length() == 0) ? "r" : options;
			//
			File link = makeLink(name);
			//
			if (Debug.DEBUG) {
				Log.d(LOG_TAG, "doConfirm: link: " + link + " uri: " + uri
						+ " options: " + options);
			}
			if (link.exists()) {
				Toast.makeText(LinkActivity.this, "Link exists",
						Toast.LENGTH_SHORT);
				return;
			}
			link.link(uri, options);

			Intent data = new Intent();
			data.setData(Uri.parse(link.toURI().toString()));
			setResult(RESULT_OK, data);
		} catch (Exception e) {
			e.printStackTrace();
			Toast
					.makeText(LinkActivity.this, e.getMessage(),
							Toast.LENGTH_LONG).show();
			setResult(RESULT_CANCELED);
		}
		//
		finish();
	}

	private File makeLink(String name) {
		if (name.endsWith(File.LINK_EXT) == false) {
			name += File.LINK_EXT;
		}
		File link = new File(path, name);
		return link;
	}

	@Override
	protected void handleFileChooseActivityResult(String sel, Intent data) {
		if (Debug.DEBUG) {
			Log.v(LOG_TAG, "selected: " + sel);
		}
		selectText.setText(sel);
		// set default link name if none exists
		if (TextUtils.isEmpty(inputText.getText())) {
			String p = Uri.parse(sel).getLastPathSegment();
			String n = (p == null || p.equals("/")) ? Utils.createName() : p;
			inputText.setText(n);
		}
	}

	@Override
	protected void handleVoiceInputActivityResult(String vi, Intent data) {
		inputText.setText(vi);
	}

}
